# coding=utf-8

import sys
import pymongo
import datetime
from pymongo import MongoClient
from bson.objectid import ObjectId
#client = MongoClient()
client = MongoClient('localhost', 27017)
db = client['test-database']
collection = db['test-collection']

#lazy
#print collection
#post = {"author": "Mike",
#    "text": "My first blog post!",
#    "tags": ["mongodb", "python", "pymongo"],
#    "date": datetime.datetime.utcnow()}

posts = db.posts
#post_id = posts.insert_one(post).inserted_id
#print post_id


print db.collection_names(include_system_collections=False)
#print posts.find_one()
#print posts.find_one({"author": "Mike"})
#print posts.find_one({"_id": ObjectId('5662d897547b8c27fcfb8ff7')})

#for post in posts.find():
#    print post

#for post in posts.find({"author": "Mike"}):
#    print post


print posts.count()
#print posts.find({"author": "Mike"}).count()

#d = datetime.datetime(2015, 12, 5, 12, 29, 19, 624000)
#for post in posts.find({"date": {"$lt": d}}).sort("author"):
#    print post

#result = db.profiles.create_index([('user_id', pymongo.ASCENDING)],
#    unique=True)
#print list(db.profiles.index_information())


user_profiles = [
   {'user_id': 211, 'name': 'Luke'},
   {'user_id': 212, 'name': 'Ziltoid'}]

#result = db.profiles.insert_many(user_profiles)
#print result

db.profiles.remove({})
new_profile = {'user_id': 213, 'name': 'Drew'}
duplicate_profile = {'user_id': 212, 'name': 'Tommy'}
result = db.profiles.insert_one(new_profile)  # This is fine.
result = db.profiles.insert_one(duplicate_profile)


db.profiles.update({}, {'$set' : {'name' : 'dog'}}, upsert=False, multi=True)

# for profile in db.profiles.find():
#    print profile


db.img.remove({})
result = db.img.create_index([('cam_id', pymongo.ASCENDING)],
    unique=True)
img_info = {"cam_id":"cam111","imgs":[{"img_name":"aaa","c_id":1,"info":"c"}]}
db.img.insert_one(img_info)

for img in db.img.find():
    print img

db.img.update({"cam_id":"cam111"}, {'$push' : {'imgs' :{"img_name":"bbb","c_id":2,"info":"e"} }}, upsert=True, multi=False)
db.img.update({"cam_id":"cam111","imgs":{"img_name":"aaa","c_id":1,"info":"c"} }, {'$set' : {'imgs.$' :{"img_name":"aaa","c_id":3,"info":"c"} }}, upsert=True, multi=False)

print "go"

for img in db.img.find():
    print img
# db.img.update({"img_name":"ok"}, {'$set' : {'img_clust_id' : 2}}, upsert=True, multi=False)

# db.img.update({"img_name":"ok2"}, {'$set' : {'img_clust_id' : 3}}, upsert=True, multi=False)

# for img in db.img.find():
#     print img